/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.ProfessionDict;
import me.zhengjie.modules.biz.service.ProfessionDictService;
import me.zhengjie.modules.biz.domain.vo.ProfessionDictQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.Date;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
 * @author yiwei.he
 * @date 2024-03-11
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "专升本指导目录（字典）")
@RequestMapping("/api/professionDict")
public class ProfessionDictController {

    private final ProfessionDictService professionDictService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportProfessionDict(HttpServletResponse response, ProfessionDictQueryCriteria criteria) throws IOException {
        professionDictService.download(professionDictService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询指导目录（字典）")
    @ApiOperation("查询指导目录（字典）")
    public ResponseEntity<PageResult<ProfessionDict>> queryProfessionDict(ProfessionDictQueryCriteria criteria, Page<Object> page){
        log.info("api/professionDict criteria:{},page:{}",criteria,page);
        return new ResponseEntity<>(professionDictService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增指导目录（字典）")
    @ApiOperation("新增指导目录（字典）")
//    @PreAuthorize("@el.check('professionDict:add')")
    public ResponseEntity<Object> createProfessionDict(@Validated @RequestBody ProfessionDict resources){
        resources.setCreateTime(new Date());
        resources.setUpdateTime(new Date());
        professionDictService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改指导目录（字典）")
    @ApiOperation("修改指导目录（字典）")
//    @PreAuthorize("@el.check('professionDict:edit')")
    public ResponseEntity<Object> updateProfessionDict(@Validated @RequestBody ProfessionDict resources){
        resources.setUpdateTime(new Date());
        professionDictService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除指导目录（字典）")
    @ApiOperation("删除指导目录（字典）")
//    @PreAuthorize("@el.check('professionDict:del')")
    public ResponseEntity<Object> deleteProfessionDict(@RequestBody List<Long> ids) {
        professionDictService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
